package com.zhuiyun.project.api.trainingTime.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuiyun.project.api.trainingTime.eneity.TrainingTime;
import com.zhuiyun.project.api.trainingTime.mapper.TrainingDateMapper;
import com.zhuiyun.project.api.trainingTime.model.TrainingTimeModel;
import com.zhuiyun.project.api.trainingTime.service.TrainingDateService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName TrainingTimeServiceImpl
 * @Description TODO 教练端-我的时间安排
 * @Author fei
 * @Date 2023/12/11 14:43
 **/
@Service
public class TrainingDateServiceImpl implements TrainingDateService {
    @Autowired
    TrainingDateMapper trainingTimeMapper;

    /**
    * @Author fei
    * @Description 添加上课时间
    * @Date 2023/12/11 15:49
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param trainingTimeModel
    */
    @Override
    public CommonResult saveTrainingTime(TrainingTimeModel trainingTimeModel) throws CommonException {
        // 查询是否已存在相同的训练时间
        QueryWrapper<TrainingTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coach_id", trainingTimeModel.getCoachId());
        queryWrapper.eq("training_time", trainingTimeModel.getTrainingTime());
        queryWrapper.eq("del_flag", 0);
        int count = trainingTimeMapper.selectCount(queryWrapper);
        if (count > 0) {
            return CommonResult.error("训练时间已存在"); // 返回时间冲突的错误码
        }
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(trainingTimeModel)){
            TrainingTime trainingTime = new TrainingTime();
            BeanUtils.copy(trainingTimeModel, trainingTime);
            Date date=new Date();
            trainingTime.setCreateTime(date);
            trainingTime.setUpdateTime(date);
            trainingTime.setDelFlag("0");
            trainingTime.setCoachId(trainingTimeModel.getCoachId());
//            trainingTime.setUserId(trainingTime.getUserId());
            int i = trainingTimeMapper.insert(trainingTime);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author fei
    * @Description 上课时间的修改方法
    * @Date 2023/12/11 15:52
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param trainingTimeModel
    */
    @Override
    public CommonResult updateTrainingTime(TrainingTimeModel trainingTimeModel) throws CommonException {
        // 查询是否已存在相同的训练时间
        QueryWrapper<TrainingTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coach_id", trainingTimeModel.getCoachId());
        queryWrapper.eq("training_time", trainingTimeModel.getTrainingTime());
        queryWrapper.eq("del_flag", 0);
        int count = trainingTimeMapper.selectCount(queryWrapper);
        if (count > 0) {
            return CommonResult.error("训练时间已存在"); // 返回时间冲突的错误码
        }
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(trainingTimeModel)){
            TrainingTime trainingTime = new TrainingTime();
            BeanUtils.copy(trainingTimeModel, trainingTime);
            Date date=new Date();
            trainingTime.setUpdateTime(date);
            trainingTime.setDelFlag("0");
            trainingTime.setCoachId(trainingTimeModel.getCoachId());
            QueryWrapper<TrainingTime> q = new QueryWrapper<>();
            q.eq("id",trainingTime.getId());
            int i = trainingTimeMapper.update(trainingTime,q);
            if(i>0){
                return CommonResult.ok("操作成功");
            }
            return CommonResult.error("操作失败");
        }else{
            return CommonResult.ok("模型参数校验失败");
        }
    }

    /**
    * @Author fei
    * @Description 删除上课时间
    * @Date 2023/12/12 19:02
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param id
    * @param coachId
    */
    @Override
    public CommonResult deleteTrainingTime(Integer id,Integer coachId) {
        // 判断id是否合法
        if(id!=null && coachId!=null){
            // 创建实体对象
            TrainingTime trainingTime = new TrainingTime();
            // 删除标志
            trainingTime.setDelFlag("1");
            // 修改时间
            trainingTime.setUpdateTime(new Date());
            // 训练时间id
            trainingTime.setId(id);
            // 教练id
            trainingTime.setCoachId(coachId);
            // 操作数据库
            int i = trainingTimeMapper.updateById(trainingTime);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok("删除成功");
            }else {
                return CommonResult.error("删除失败");
            }
        }
        return CommonResult.error("id不可为null");
    }


}
